/**
 * 
 */
package com.manolo.game.card.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import com.manolo.framework.connection.entity.Message;
import com.manolo.framework.connection.entity.Player;
import com.manolo.framework.connection.server.ServerPlayer;
import com.manolo.framework.deck.Card;
import com.manolo.framework.deck.Deck;

/**
 * @author gustavo
 * 
 */
public class ServerCard {

	private int port;

	private ServerSocket server = null;
	private int numConectionMax = 2;
	private int numConectionCount = 0;
	private HashMap<Integer, Player> players;
	private int stage; // 0 wait connection. 1 star game, 2 gaming

	public ServerCard() {
		stage = 0;
		players = new HashMap<Integer, Player>();
		port = 4555;
		try {
			server = new ServerSocket(port);
		} catch (IOException e) {
			System.out.println("Could not listen on port " + port);
			System.exit(-1);
		}
	}

	public boolean getDeck = false;

	public void getStartDeck() {
		Deck deck = new Deck(1, 3, 4);
		System.out.println(deck.getDeck().size());
		// Shuffle the Deck
		deck.shuffleDeck();
		// Cut the deck at the position 2
		deck.cutDeck(22);

		ArrayList<List<Card>> allHands = deck.distributeCards();

		/*
		 * The code below show all four hand distributed This was written to
		 * test the methods above, you can remove it that the class will work
		 * normally
		 */

		System.out.println("----------------");
		int contHands = 0;
		Set<Integer> set = players.keySet();
		System.out.println("NUmero de players na A:" + set.size());

		for (Integer index : set) {
			players.get(index)
					.getMessages()
					.add(new Message(index, 0, "Server ", "Game Start " + index));
			synchronized (this.players.get(index)) {
				this.players.get(index).notifyAll();
			}

		}

		for (Integer index : set) {
			players.get(index).getCards().addAll(allHands.get(index));
			synchronized (this.players.get(index)) {
				this.players.get(index).notifyAll();
			}
		}

	}

	public void startGame() {
		// stage = 2;
		//
		if (!getDeck) {
			System.out.println("dar cartas");
			getStartDeck();

			getDeck = true;

		}

		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void listing() {
		// game life cicle
		while (true) {
			// wait connection 2 players
			switch (stage) {
			case 0:
				System.out.println("Stage wait connection");
				waitingConnetion();
				break;
			case 1:
				System.out.println("Start game");
				startGame();
				break;
			case 2:
			default:
				break;
			}
		}

	}

	public void waitingConnetion() {
		if (numConectionCount < numConectionMax) {
			ServerPlayer player;
			try {
				player = new ServerPlayer(numConectionCount++, server.accept(),
						players);
				System.out.println(numConectionCount);
				Thread t = new Thread(player);
				t.start();
			} catch (IOException e) {
				System.out.println("Accept failed: 4444");
				System.exit(-1);
			}
			System.out.println("wait connection :: " + numConectionCount);
		}

		if (numConectionCount == numConectionMax)
			stage = 1;
	}

	public static void main(String[] args) {
		ServerCard gameServer = new ServerCard();
		gameServer.listing();

	}
}
